package de.luh.slimpp

import com.google.inject.Inject
import de.luh.slimpp.slimPP.Model
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.junit.Test
import static org.junit.Assert.*
import org.junit.runner.RunWith

@RunWith(typeof(XtextRunner))
@InjectWith(typeof(SlimPPInjectorProvider))
class InternalTypeDatabaseTest {
	@Inject extension ParseHelper<Model>

	@Test def testFindClass() {
		val model = '''class A   class a.A   class a.B   class a.b.A   class a.b.B'''.parse
		val db = new InternalTypeDatabase(model)

		val A = db.findClass('A')
		val aA = db.findClass('a.A')
		val aB = db.findClass('a.B')
		val abA = db.findClass('a.b.A')
		val abB = db.findClass('a.b.B')

		assertNotNull(A)
		assertNotNull(aA)
		assertNotNull(aB)
		assertNotNull(abA)
		assertNotNull(abB)

		assertEquals('A', A.qualifiedName.name)
		assertEquals(0, A.qualifiedName.namespaces.size)

		assertEquals(1, aA.qualifiedName.namespaces.size)
		assertEquals('a', aA.qualifiedName.namespaces.head)
		assertEquals('A', aA.qualifiedName.name)

		assertEquals(1, aB.qualifiedName.namespaces.size)
		assertEquals('a', aB.qualifiedName.namespaces.head)
		assertEquals('B', aB.qualifiedName.name)

		assertEquals(2, abA.qualifiedName.namespaces.size)
		assertEquals('a', abA.qualifiedName.namespaces.head)
		assertEquals('b', abA.qualifiedName.namespaces.tail.head)
		assertEquals('A', abA.qualifiedName.name)

		assertEquals(2, abB.qualifiedName.namespaces.size)
		assertEquals('a', abB.qualifiedName.namespaces.head)
		assertEquals('b', abB.qualifiedName.namespaces.tail.head)
		assertEquals('B', abB.qualifiedName.name)

		assertEquals(A, db.findClass('A', A))
		assertEquals(aA, db.findClass('A', aA))

		assertEquals(aB, db.findClass('B', aA))
		assertEquals(aB, db.findClass('a.B', aA))
		
		assertEquals(abA, db.findClass('A', abB))
	}
}
